// components/item-card/item-card.js
import formatTime from '../../utils/formatTime.js'
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: Object,
    userInfo: Object
  },
  options: {
    
  },
  observers: {
    ['item.createTime'](val) {
      if (val) {
        //console.log(val)
        this.setData({
          createTime: formatTime(new Date(val.replace(/-/g,'/'))).substr(0,16)
        })
      }
    },
    ['item.watchNum'](val) {
      this.setData({
        watchNum: val? this.formatNum(val):'0'
      })
    },
    ['item.commentNum'](val) {
      this.setData({
        commentNum: val? this.formatNum(val):'0'
      })
    },
    ['item.img'](val) {
      this.setData({
        imgList: val? val.split(","):[]
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _createTime: '',
    _watchNum: '',
    _commentNum: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatNum(num) {
      if (num < 1000) {
        return num.toString()
      } else if (num<10000) {
        let numStr = num.toString()
        return numStr[0]+'.'+numStr[1] + 'k'
      } else {
        return '1w+'
      }
    },
    deleteInvitation(e){
      let _url = req.actions.ym+ "invitation/deleteInvitation",params={invitationId:e.currentTarget.dataset.id},that=this
      wx.showModal({
        title: '提示',
        content: '确定要删除吗？',
        success: function (sm) {
          if (sm.confirm) {
            utils.myResquest(params,_url,function(res){
              console.log("deleteInvitation===>"+JSON.stringify(res))
              wx.showToast({title: '删除成功！'})
              that.triggerEvent('callParent', {type:'deleteInvitation',id:e.currentTarget.dataset.id})
            })
          }
        }
      })
      
    },
    overInvitation(e){
      let _url = req.actions.ym+ "invitation/overInvitation",params={invitationId:e.currentTarget.dataset.id},that=this
      wx.showModal({
        title: '提示',
        content: '确定要结束此贴吗？',
        success: function (sm) {
        utils.myResquest(params,_url,function(res){
          console.log("overInvitation===>"+JSON.stringify(res))
          wx.showToast({title: '结贴成功！'})
          that.triggerEvent('callparent', {type:'overInvitation',id:e.currentTarget.dataset.id})
        })
      }
    })
      
    }
  }
})

